package com.example.hyw.szysteam.model;

import com.example.hyw.szysteam.POJO.HttpResult;
import com.example.hyw.szysteam.POJO.Practice;
import com.example.hyw.szysteam.arithmetic.AcMain;
import com.example.hyw.szysteam.http.RetrofitManager;

import java.util.ArrayList;

import io.reactivex.Observable;
import io.reactivex.ObservableEmitter;
import io.reactivex.ObservableOnSubscribe;

/**
 * Created by HYW on 2018/6/12.
 */

public class PracticeModel {
    public Observable<ArrayList<Practice>> initPractice(final int number, final int operator, final int range){
        return Observable.create(new ObservableOnSubscribe<ArrayList<Practice>>() {
            @Override
            public void subscribe(ObservableEmitter<ArrayList<Practice>> emitter) throws Exception {
                AcMain acMain=new AcMain();
                ArrayList<Practice> practices=acMain.initPractice(number,operator,range);
                emitter.onNext(practices);
                emitter.onComplete();
            }
        });
    }

    public Observable<HttpResult<ArrayList<Practice>>> getPractice(final Integer testid){
        return RetrofitManager.getInstance().getRetrofitApiService().getpractice(testid);
    }

    public Observable<Integer> correct(final ArrayList<Practice> practices, final ArrayList<String> answer){
        return Observable.create(new ObservableOnSubscribe<Integer>() {
            @Override
            public void subscribe(ObservableEmitter<Integer> emitter) throws Exception {
                int count=0;
                for (int i=0;i<answer.size();i++){
                    if (practices.get(i).getAnswer().equals(answer.get(i))){
                        count++;
                    }
                }
                int score=(100*count)/answer.size();
                emitter.onNext(score);
                emitter.onComplete();
            }
        });
    }
}
